<html>
<h1> Web Query Interface </h1>
<body>
</br>
<p>
Attempt your favorite SQL query:
</p>
</br>
<form method="GET">
<TEXTAREA NAME='query' ROWS=8 COLS=60>
<?php if($_GET["query"]) echo $_GET["query"] ?></TEXTAREA>
<input type="submit" value="Submit!">
</form>
</br>

<?php
error_reporting(0);
$query = $_GET["query"];
if($query) {
	$connection = mysql_connect("localhost", "cs143", "");
	if(!$connection) {
		$errmsg = mysql_error($connection);
		print "Connection failure: $errmsg <br />";
		exit(1);
	}
	mysql_select_db("CS143", $connection);
	
	$result = mysql_query($query, $connection);
	if(!$result) {
		print "Query error: ".mysql_error();
		exit(1);
	}
	
	print "<h3> Results from Query:  </h3>";
	print "<table border = 1 cellspacing = 2 cellpadding = 2>\n";
	# fetches the titles associated with the result to put in the
	# 	the first line of the table
	$i = 0;
	print "<tr align=center>";
	while ($i < mysql_num_fields($result)) {
    $field = mysql_fetch_field($result, $i);
		if ($field) 
		{
			$colname = $field->name;
			print "<th><b> $colname </b></th>";	
		}
		$i++;
    }
	print "</tr>\n";
	
	# prints the rest of the lines of data into the table
	while($row = mysql_fetch_row($result)) {
		print "<tr align=center>";
		for($col=0; $col < count($row); $col++) {
			if($row[$col] != "")
				print "<td> $row[$col] </td>";
			else
				print "<td> N/A </td>";
		}
		print "</tr>\n";
	}
	print "</table>";
	
	mysql_close($connection);
}

?>
</body>
</html>